package com.oreilly.android.taskmanager;

import java.io.IOException;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.oreilly.android.taskmanager.views.AddressOverlay;

public class AddLocationMapActivity extends MapActivity {

	public static final String ADDRESS_RESULT = "address";
	private EditText addressText;
	private MapView mapView;
	private MyLocationOverlay myLocationOverlay;
	private AddressOverlay addressOverlay;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.add_location);
		setUpViews();
	}

	@Override
	protected void onResume() {
		super.onResume();
		myLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onPause() {
		super.onPause();
		myLocationOverlay.disableMyLocation();
	}

	private void setUpViews() {
		Drawable redPushPin = getResources().getDrawable(
				R.drawable.red_push_pin);
		addressOverlay = new AddressOverlay(redPushPin);
		addressText = (EditText) findViewById(R.id.task_address);
		Button mapLocationButton = (Button) findViewById(R.id.map_location_button);
		Button useLocationButton = (Button) findViewById(R.id.use_location_button);

		mapView = (MapView) findViewById(R.id.map);

		myLocationOverlay = new MyLocationOverlay(this, mapView);
		mapView.getOverlays().add(myLocationOverlay);
		mapView.invalidate();

		mapView.setBuiltInZoomControls(true);

		mapLocationButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				searchAddresses();
			}
		});
		useLocationButton.setOnClickListener(new UseThisLocation());
		// useCurrentButton.setOnClickListener(new UseCurrentLocation());
	}

	@Override
	protected boolean isLocationDisplayed() {
		return super.isLocationDisplayed();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void searchAddresses() {
		String searchAddress = addressText.getText().toString();
		Geocoder g = new Geocoder(this);
		List<Address> addresses;

		mapView.getOverlays().clear();

		try {
			addresses = g.getFromLocationName(searchAddress, 3);
			if (addresses != null && addresses.size() > 0) {
				addressOverlay.setAddresses(addresses);
				List<Overlay> mapOverlays = mapView.getOverlays();

				GeoPoint geo = addressOverlay.getCenter();
				centerOnGeoPoint(geo);

				mapOverlays.add(addressOverlay);
				mapOverlays.add(myLocationOverlay);
				mapView.invalidate();

			} else {
				Toast.makeText(this, "No Addresses Found", 5000).show();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void centerOnGeoPoint(GeoPoint geoPoint) {
		final MapController mapController = mapView.getController();
		mapController.animateTo(geoPoint, new Runnable() {
			public void run() {
				mapController.setZoom(17);
			}
		});
	}

	private class UseCurrentLocation implements View.OnClickListener {

		public void onClick(View v) {
			// TaskManagerApplication app = (TaskManagerApplication)
			// getApplication();
			// Location location = app.getLocationManager().getLatestLocation();
			// address.setLatitude(location.getLatitude());
			// address.setLongitude(location.getLongitude());
			// Intent intent = new Intent();
			// intent.putExtra(ADDRESS_RESULT, address);
			// setResult(RESULT_OK, intent);
			//
			// finish();
		}

	}

	private class UseThisLocation implements View.OnClickListener {

		public void onClick(View v) {
			Address address = addressOverlay.getSelectedAddress();
			if (null != address) {
				Intent intent = new Intent();
				intent.putExtra(ADDRESS_RESULT, address);
				setResult(RESULT_OK, intent);
			}

			finish();
		}
	}
}
